﻿using MoolinkHsmsServer;
using SuperSocket;
using SuperSocket.WebSocket;

using SuperSocket.SessionContainer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using SuperSocket.WebSocket.Server;
using Moolink.Helper;
using System.IO;
using superSocketApi.DataBase;

namespace superSocketApi.Global
{
	public static class DeviceInfoFun
	{
		public class DeviceModel
		{
			public string DeviceId { get; set; }
			public string DeviceType { get; set; }
		}
		public static List<DeviceModel> DeviceList()
		{
			List<DeviceModel> list = new List<DeviceModel>();

			Dictionary<string, string> dlist = RedisHelper.GetHashValue("YaXinDevice", GlobalSystem.RedisDB_YaXinDevice);
			if (dlist != null && dlist.Count > 0)
			{

				foreach (var item in dlist)
				{
					//list.Add((item.RemoteEndPoint as IPEndPoint).Address.ToString() + "," + item.SessionID + "," + item["DeviceId"] + "," + item.DataContext + "," + item.LastActiveTime.ToString("yyyy-MM-dd HH:mm:ss"));
					list.Add(new DeviceModel
					{
						DeviceId = item.Key.ToString(),
						DeviceType = item.Value.ToString(),
					});
					//item.SendAsync(Encoding.UTF8.GetBytes("hello," + item.SessionID));
				}
			}

			return list;
		}

		public static string SendFileReq(ushort deviceId, string sessionId, string _filename)
		{

			if (File.Exists(@"D:\wse-test\yxw\files\" + _filename))
			{
				FileInfo fileInfo = new FileInfo(@"D:\wse-test\yxw\files\" + _filename);
				GlobalSystem.deviceIdSendFileName[deviceId] = _filename;
				HsmsMessageLength fileLength = new HsmsMessageLength { Length = (UInt32)fileInfo.Length };
				//Structure: L, 2
				//			1. < A  PPID >
				//			2. < U4 LENGTH >
				//01 02 41 08 4E 50 47 4D 30 2E 50 57 B1 04 00 1A F5 14
				//01 02 41 09 4E 50 47 4D 30 2E 50 57 62 B1 04 00 1A F5 14
				List<byte> dat = new List<byte>();
				dat.AddRange(new byte[4] { 0x01, 0x02, 0x41, (byte)_filename.Length });
				dat.AddRange(System.Text.Encoding.ASCII.GetBytes(_filename));
				dat.AddRange(new byte[2] { 0xB1, 0x04 });
				dat.AddRange(fileLength.Bytes);
				return Global.ServerInfoFun.SendHsmsDataBySessionId(sessionId, 7, 1, dat.ToArray().ByteArrayGetHexString());
				//return ("File:"+_filename + ",Size:"+fileInfo.Length.ToString() + " Add To SendFileTask");
			}
			else
			{
				//File.WriteAllBytes(@"D:\wse-test\yxw\files\" + _filename, msg._p.TextFrame.GetFileBytes());
				return (_filename + " is not Exists");
			}
		}
		public static string GetFileReq(ushort deviceId, string sessionId, string _filename)
		{
			List<byte> dat = new List<byte>();
			dat.AddRange(new byte[2] { 0x41, (byte)_filename.Length });
			dat.AddRange(System.Text.Encoding.ASCII.GetBytes(_filename));
			return Global.ServerInfoFun.SendHsmsDataBySessionId(sessionId, 7, 5, dat.ToArray().ByteArrayGetHexString(), true, false); //返回需较长时间,因些不等待返回		  
		}

		public static string SendCtrlCmd(ushort deviceId, string sessionId, byte cmdval,string password)
		{
			if (rtDatOpt.CheckWbAdminPasswordDatViewByCommMachineNumber(deviceId, password))
				return Global.ServerInfoFun.SendHsmsDataBySessionId(sessionId, 2, 21, "A5 01 " + cmdval.ToString("X2"));
			else
				return ("password check Err");
		}


		//public static string DelFileReq(ushort deviceId, string sessionId, string _filename)
		//{

		//}





	}
}
